
package com.suarte.core.hibernate;
import com.suarte.core.Company;
import com.suarte.core.Contact;
import com.suarte.core.dao.ContactDao;
import java.util.ArrayList;
import java.util.List;

import org.appfuse.dao.hibernate.GenericDaoHibernate;

import org.springframework.stereotype.Repository;

/**
 * @date   Dec 20, 2010
 * @author Ggutierrez
 */
@Repository("contactDao")
public class ContactDaoHibernate extends GenericDaoHibernate<Contact, Long> implements ContactDao {
    
    public ContactDaoHibernate() {
        super(Contact.class);
    }

    public List<Contact> findByNames(String firstName, String lastName) {
        String hql = "from Contact this where 1 = 1";

        List args = new ArrayList();

        if (firstName != null) {
            args.add(firstName.concat("%"));
            hql += " and upper(this.firstName) like upper(?)";
        }

        if (lastName != null) {
            args.add(lastName.concat("%"));
            hql += " and upper(this.lastName) like upper(?)";
        }

        List results = getHibernateTemplate().find(hql, args.toArray());
        return results;
    }

    public List<Contact> findByCompany(Company company) {
        return getHibernateTemplate().find("from Contact this where this.company = ? order by this.firstName asc", company);
    }

    public List<Contact> findContacts() {
        return getHibernateTemplate().find("from Contact this order by this.firstName asc");
    }
}
